<?php
/**
 * NetBiz Framework
 * Copyright (C) 20009 - 2010 NetBiz
 * 
 * All rights reserved.
 */
function __autoload($classname)
{
	// Are we trying to load a framework package class?
	if (strpos($classname,'FRM_') === 0)
	{
		$filename = FRAMEWORK_DIR.'/class';
		$classname = str_replace('FRM_','',$classname);
	}
	else
	{
		// Are we trying to load a controller?
		if (strrchr($classname,'Controller') == 'Controller')
		{
			$filename = APPLICATION_DIR.'/controller';
		}
		else
		{
			$filename = APPLICATION_DIR.'/business';
		}
	}

	// Add package access
	$filename .= '/' . str_replace('_','/',$classname) . '.php';

	if (file_exists($filename)) {
		require_once($filename);
	}
}


require_once 'PEAR.php';

class FRM extends PEAR {
	static $_registry;
	
	static public function _initRegistry()
	{
		FRM::$_registry = array();
	}
	
	static public function register($object_name, $object)
	{
		if (FRM::$_registry === null) {
			FRM::_initRegistry();
		}
		
		// Try first to check if it's a config set action
		if (strpos($object_name, '_') !== FALSE)
		{
			list($domain,$name) = explode('_',$object_name,2);
			if (FRM_Config::exists($domain,$name))
			{
				FRM_Config::set($domain,$name,$object);
				return;
			}
		}
		
		FRM::$_registry[$object_name] = $object;
	}
	
	static public function load($object_name)
	{
		if (FRM::$_registry === null) {
			FRM::_initRegistry();
		}
		
		if (!isset(FRM::$_registry[$object_name]))
		{
			if (strpos($object_name,'_') !== FALSE)
			{
				try
				{
					list($domain,$name) = explode('_',$object_name,2);
					return FRM_Config::get($domain,$name);
				}
				catch (FRM_Exception $e)
				{
					// Next exception will be sent
				}
			}

			throw new FRM_Exception("Cannot find variable `$object_name` in registry.", FRM_Exception::REGISTRY_VAR);
		}
		
		return FRM::$_registry[$object_name];
	}

	// Helper functions part
	static public function redirect($url)
	{
		header("Location: " . $url);
		exit();
	}
}
?>